
import {
  getRepEventDrivenAcarsUplinkApi,
  addEventDrivenAcarsUplinkApi,
  updateRepEventDrivenAcarsUplinkApi,
  delRepEventDrivenAcarsUplinkApi,
  getCmcEventListApi
} from '@/services/eventTriggerPacketUpload.js';
import {
  findListByFleetApi
} from '@/services/users.js';

import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    repEventDrivenAcarsUplinkLoading: false,
    repEventDrivenAcarsUplinkList: [],
    addEventDrivenAcarsUplinkLoading: false,
    repUplinkRequest: [],
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 10
    },
    cmcEventList: [],
    findListByFleetData: [],
    findListByFleetLoading: false
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取故障基础信息列表
    async getRepEventDrivenAcarsUplink({ commit, state, dispatch }, params) {
      commit('setState', { key: 'repEventDrivenAcarsUplinkLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getRepEventDrivenAcarsUplinkApi(apiParams);
      dispatch('setting/setAcarsTime', {}, { root: true });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'repEventDrivenAcarsUplinkList', payload: rep.data.list });
      commit('setState', { key: 'repEventDrivenAcarsUplinkLoading', payload: false });
    },

    async getCmcEventList({ commit, state }) {
      const rep = await getCmcEventListApi({ type: 4 });
      const newData = (rep.data ?? []).sort((a, b) => (a?.name ?? 0).localeCompare((b?.name ?? 0)));
      commit('setState', { key: 'cmcEventList', payload: newData });
    },
    // 新增
    async addEventDrivenAcarsUplink({ commit }, params) {
      commit('setState', { key: 'addEventDrivenAcarsUplinkLoading', payload: true });
      try {
        const rep = await addEventDrivenAcarsUplinkApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addEventDrivenAcarsUplinkLoading', payload: false });
      }
    },
    // 删除
    async delRepEventDrivenAcarsUplink({ commit }, params) {
      try {
        await delRepEventDrivenAcarsUplinkApi(params);
        // if (rep.data === '成功') {
        message.success('操作成功');
        return Promise.resolve(true);
        // } else {
        //   message.error(rep.message);
        // }
      } catch (error) {
        message.error('操作失败');
      }
    },

    // 修改
    async updateRepEventDrivenAcarsUplink({ commit }, params) {
      commit('setState', { key: 'addEventDrivenAcarsUplinkLoading', payload: true });
      try {
        const rep = await updateRepEventDrivenAcarsUplinkApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addEventDrivenAcarsUplinkLoading', payload: false });
      }
    },
    async findListAllByFleet({ commit }, params) {
      commit('setState', { key: 'findListByFleetLoading', payload: true });
      const f1 = findListByFleetApi({ fleet: 'B787-9' });
      const f2 = findListByFleetApi({ fleet: 'B787-8' });
      const rep = await Promise.all([f1, f2]);
      const repData = [
        {
          id: Math.random().toString(32),
          tailnumber: 'B787-9',
          children: rep[0]?.data ?? []

        },
        {
          id: Math.random().toString(32),
          tailnumber: 'B787-8',
          children: rep[1]?.data ?? []

        }
      ];
      console.log(repData);
      commit('setState', { key: 'findListByFleetData', payload: repData });
      commit('setState', { key: 'findListByFleetLoading', payload: false });
    }
  }
};
